<?php
declare (strict_types=1);

namespace app\model\adv;

use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Model;
use think\model\relation\HasMany;

/**
 * @property int $agent_id 代理id
 * @property int $id
 * @property int $num 容量
 * @property int $status 状态 0 暂停 1 启用
 * @property string $created_at
 * @property string $pixel 像素
 * @property string $title 广告位置说明
 * @property string $updated_at
 * @property-read \app\model\adv\AdvList[] $adv_list
 */
class Adv extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'adv';

    use ModelTrait;

    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * @param Model $query
     * @param $value
     */
    public function searchTitleAttr($query, $value)
    {
        if ($value) {
            $query->whereLike('title', "%$value%");
        }
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchStatusAttr($query, $value)
    {
        if ($value && $value != '') {
            $query->where('status', $value);
        }
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchAgentIdAttr($query, $value)
    {
        if ($value) {
            $query->where('agent_id', $value);
        }
    }

    /**
     * 广告列表
     * @return HasMany
     */
    public function advList(): HasMany
    {
        return $this->hasMany(AdvList::class, 'adv_id', 'id');
    }
}
